<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="唯一项目ID" prop="nid">
        <el-input
          v-model="queryParams.nid"
          placeholder="请输入唯一项目ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间戳" prop="stamp">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.stamp"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择时间戳">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户id" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作名称" prop="opt">
        <el-input
          v-model="queryParams.opt"
          placeholder="请输入操作名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作内容" prop="ctn">
        <el-input
          v-model="queryParams.ctn"
          placeholder="请输入操作内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结果" prop="res">
        <el-input
          v-model="queryParams.res"
          placeholder="请输入结果"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程跳转至用户" prop="flowToUserid">
        <el-input
          v-model="queryParams.flowToUserid"
          placeholder="请输入流程跳转至用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程跳转至角色" prop="flowToRoleid">
        <el-input
          v-model="queryParams.flowToRoleid"
          placeholder="请输入流程跳转至角色"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程跳转至单位" prop="flowToDeptid">
        <el-input
          v-model="queryParams.flowToDeptid"
          placeholder="请输入流程跳转至单位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前节点K" prop="currNodeK">
        <el-input
          v-model="queryParams.currNodeK"
          placeholder="请输入当前节点K"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前节点" prop="currNode">
        <el-input
          v-model="queryParams.currNode"
          placeholder="请输入当前节点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下一节点K" prop="nextNodeK">
        <el-input
          v-model="queryParams.nextNodeK"
          placeholder="请输入下一节点K"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下一节点" prop="nextNode">
        <el-input
          v-model="queryParams.nextNode"
          placeholder="请输入下一节点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan"  size="mini" @click="handleQuery">搜索</el-button>
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="20" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['standard:flow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['standard:flow:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['standard:flow:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['standard:flow:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="left" />
      <el-table-column label="下一节点" align="left" prop="id" />
      <el-table-column label="唯一项目ID" align="left" prop="nid" />
      <el-table-column label="时间戳" align="left" prop="stamp" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stamp, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户id" align="left" prop="userid" />
      <el-table-column label="操作名称" align="left" prop="opt" />
      <el-table-column label="操作内容" align="left" prop="ctn" />
      <el-table-column label="结果" align="left" prop="res" />
      <el-table-column label="意见" align="left" prop="opn" />
      <el-table-column label="流程跳转至用户" align="left" prop="flowToUserid" />
      <el-table-column label="流程跳转至角色" align="left" prop="flowToRoleid" />
      <el-table-column label="流程跳转至单位" align="left" prop="flowToDeptid" />
      <el-table-column label="当前节点K" align="left" prop="currNodeK" />
      <el-table-column label="当前节点" align="left" prop="currNode" />
      <el-table-column label="下一节点K" align="left" prop="nextNodeK" />
      <el-table-column label="下一节点" align="left" prop="nextNode" />
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['standard:flow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['standard:flow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改流程记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="唯一项目ID" prop="nid">
          <el-input v-model="form.nid" placeholder="请输入唯一项目ID" />
        </el-form-item>
        <el-form-item label="时间戳" prop="stamp">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.stamp"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择时间戳">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户id" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="操作名称" prop="opt">
          <el-input v-model="form.opt" placeholder="请输入操作名称" />
        </el-form-item>
        <el-form-item label="操作内容" prop="ctn">
          <el-input v-model="form.ctn" placeholder="请输入操作内容" />
        </el-form-item>
        <el-form-item label="结果" prop="res">
          <el-input v-model="form.res" placeholder="请输入结果" />
        </el-form-item>
        <el-form-item label="意见" prop="opn">
          <el-input v-model="form.opn" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="流程跳转至用户" prop="flowToUserid">
          <el-input v-model="form.flowToUserid" placeholder="请输入流程跳转至用户" />
        </el-form-item>
        <el-form-item label="流程跳转至角色" prop="flowToRoleid">
          <el-input v-model="form.flowToRoleid" placeholder="请输入流程跳转至角色" />
        </el-form-item>
        <el-form-item label="流程跳转至单位" prop="flowToDeptid">
          <el-input v-model="form.flowToDeptid" placeholder="请输入流程跳转至单位" />
        </el-form-item>
        <el-form-item label="当前节点K" prop="currNodeK">
          <el-input v-model="form.currNodeK" placeholder="请输入当前节点K" />
        </el-form-item>
        <el-form-item label="当前节点" prop="currNode">
          <el-input v-model="form.currNode" placeholder="请输入当前节点" />
        </el-form-item>
        <el-form-item label="下一节点K" prop="nextNodeK">
          <el-input v-model="form.nextNodeK" placeholder="请输入下一节点K" />
        </el-form-item>
        <el-form-item label="下一节点" prop="nextNode">
          <el-input v-model="form.nextNode" placeholder="请输入下一节点" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFlow, getFlow, delFlow, addFlow, updateFlow, exportFlow } from "@/api/standard/flow";

export default {
  name: "Flow",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程记录表格数据
      flowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nid: null,
        stamp: null,
        userid: null,
        opt: null,
        ctn: null,
        res: null,
        opn: null,
        flowToUserid: null,
        flowToRoleid: null,
        flowToDeptid: null,
        currNodeK: null,
        currNode: null,
        nextNodeK: null,
        nextNode: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nid: [
          { required: true, message: "唯一项目ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询流程记录列表 */
    getList() {
      this.loading = true;
      listFlow(this.queryParams).then(response => {
        this.flowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nid: null,
        stamp: null,
        userid: null,
        opt: null,
        ctn: null,
        res: null,
        opn: null,
        flowToUserid: null,
        flowToRoleid: null,
        flowToDeptid: null,
        currNodeK: null,
        currNode: null,
        nextNodeK: null,
        nextNode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加流程记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFlow(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改流程记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFlow(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFlow(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除流程记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delFlow(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有流程记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportFlow(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
